﻿/// <reference path="utils.js" />
'use strict';

define([
    'backbone',
], function (Backbone) {
    var Model = Backbone.Model.extend({
        defaults: {
            id: null,
            customerId: null,
            datetime: null,
            pay: 0,
            sealed: false,
        },

        fetch: function (params, options) {
            var that = this;

            if (!options) options = {};

            App.mainService.runProcedure(
                {
                    Procedure: App.config.databases.billing + '.BILL.GetBill',
                    Parameters: (params instanceof Array) ? params : utils.backend.objToQueryParams(params),
                },
                _.extend({}, options, {
                    success: function (resp) {
                        try {
                            if (resp.Success == true) {
                                if (resp.Data) {
                                    var data = resp.Data[0][0];

                                    that.set({
                                        id: data.Id,
                                        customerId: data.CustomerId,
                                        datetime: data.Datetime,
                                        pay: data.Pay,
                                        sealed: data.Sealed,
                                    });

                                    if (options.success)
                                        options.success(that, resp);
                                }
                                else {
                                    console.debug('data null');

                                    if (options.error)
                                        options.error('data null');
                                }
                            }
                            else {
                                console.debug(resp.Message);

                                if (options.error)
                                    options.error(resp.Message);
                            }
                        }
                        catch (Error) {
                            if (options.async) {
                                console.error(Error);

                                if (options.error)
                                    options.error('Exception');
                            }
                            else {
                                throw Error;
                            }
                        }
                    },
                    error: function () {
                        console.debug('error');

                        if (options.error)
                            options.error('error');
                    },
                    //async: false,
                })
            );

            return this;
        },
        reset: function () {
            this.set(Model.prototype.defaults, { silet: true, })
                .trigger('reset');
        },
    });

    return Model;
});